//
//  EmoticonAttachment.swift
//  表情键盘
//
//  Created by 雄 on 15/7/21.
//  Copyright © 2015年 雄. All rights reserved.
//

import UIKit

class EmoticonAttachment: NSTextAttachment {

    // 表情附件文字
    var chs: String?
    
    class func emoticonStr(emoticon: Emoticon, font: UIFont) -> NSAttributedString {
        // 1. 表情文本组件
        let imageAttachment = EmoticonAttachment()
        imageAttachment.chs = emoticon.chs
        imageAttachment.image = UIImage(named: emoticon.imagePath!)
        let h = font.lineHeight
        imageAttachment.bounds = CGRectMake(0, -4, h, h)
        
        // 2. 设置表情文本
        let imageText = NSAttributedString(attachment: imageAttachment)
        
        // 设置表情文本字体,传进来的文本字体一致
        let imageTextM = NSMutableAttributedString(attributedString: imageText)
        imageTextM.addAttributes([NSFontAttributeName: font], range: NSRange(location: 0, length: 1))
        
        return imageTextM
        
    }
}
